home *** CD-ROM | disk | FTP | other *** search
/ More SimCities / More SimCities (1997).iso / sc / tools / simutil / simunit.pas next >
Pascal/Delphi Source File  |  1997-06-17  |  15KB  |  401 lines

  1. Unit SimUnit;
  2. {
  3.   Reads a SimCity .CTY file, reports its various parameters, and
  4.   allows one to change them
  5. }
  6. Interface
  7.  
  8. Type
  9.   Str64 = String[64];
  10.   Str40  = String[40];
  11.   Str2   = String[2];
  12.  
  13.   {Holds the lower 10 bits of the map tile integer}
  14.   MapTileIndex_Rec = Record
  15.                        ClearTerrain : word;
  16.                        Water        : word;
  17.                        River        : word;
  18.                        RiverEdge    : word;
  19.                        TreeEdge     : word;
  20.                        Trees        : word;
  21.                        Parks        : word;
  22.                        Rubble       : word;
  23.                        Flood        : word;
  24.                        Radiation    : word;
  25.                        Fire         : word;
  26.                        Road_NoTraf  : word;
  27.                        Road_LtTraf  : word;
  28.                        Road_HvTraf  : word;
  29.                        Power        : word;
  30.                        Transit      : word;
  31.                        EmptyRes     : word;
  32.                        Houses       : word;
  33.                        ResLoVal1    : word; {1 = low density to highest density}
  34.                        ResLoVal2    : word;
  35.                        ResLoVal3    : word;
  36.                        ResLoVal4    : word;
  37.                        ResMidVal1   : word;
  38.                        ResMidVal2   : word;
  39.                        ResMidVal3   : word;
  40.                        ResMidVal4   : word;
  41.                        ResUpVal1    : word;
  42.                        ResUpVal2    : word;
  43.                        ResUpVal3    : word;
  44.                        ResUpVal4    : word;
  45.                        ResHiVal1    : word;
  46.                        ResHiVal2    : word;
  47.                        ResHiVal3    : word;
  48.                        ResHiVal4    : word;
  49.                        Hospital     : word;
  50.                        Church       : word;
  51.                        EmptyCom     : word;
  52.                        ComLoVal1    : word;
  53.                        ComLoVal2    : word;
  54.                        ComLoVal3    : word;
  55.                        ComLoVal4    : word;
  56.                        ComLoVal5    : word;
  57.                        ComMidVal1   : word;
  58.                        ComMidVal2   : word;
  59.                        ComMidVal3   : word;
  60.                        ComMidVal4   : word;
  61.                        ComMidVal5   : word;
  62.                        ComUpVal1    : word;
  63.                        ComUpVal2    : word;
  64.                        ComUpVal3    : word;
  65.                        ComUpVal4    : word;
  66.                        ComUpVal5    : word;
  67.                        ComHiVal1    : word;
  68.                        ComHiVal2    : word;
  69.                        ComHiVal3    : word;
  70.                        ComHiVal4    : word;
  71.                        ComHiVal5    : word;
  72.                        IndEmpty     : word;
  73.                        IndLoVal1    : word;
  74.                        IndLoVal2    : word;
  75.                        IndLoVal3    : word;
  76.                        IndLoVal4    : word;
  77.                        IndHiVal1    : word;
  78.                        IndHiVal2    : word;
  79.                        IndHiVal3    : word;
  80.                        IndHiVal4    : word;
  81.                        SeaPort      : word;
  82.                        Airport      : word;
  83.                        CoalPower    : word;
  84.                        FireStation  : word;
  85.                        PoliceStation: word;
  86.                        StadiumEmpty : word;
  87.                        StadiumFull  : word;
  88.                        NuclearPower : word;
  89.                        OpenH_Bridge : word;
  90.                        RadarDish    : word;
  91.                        Fountain     : word;
  92.                        OpenV_Bridge : word;
  93.                      end;
  94.  
  95.   {Holds the upper 6 bits of the map tile integer}
  96.   MapTileAttr_Rec = Record
  97.                       ZoneCenter   : Boolean;
  98.                       Animated     : Boolean;
  99.                       Bulldozable  : Boolean;
  100.                       Burnable     : Boolean;
  101.                       ConductPower : Boolean;
  102.                       Powered      : Boolean;
  103.                     end;
  104.  
  105.   {Misc variables in the SimCity File}
  106.   MiscVar_Rec    = record
  107.                     filler        : word;
  108.                     ExtMktSize    : word;
  109.                     ResPop        : word;
  110.                     ComPop        : word;
  111.                     IndPop        : word;
  112.                     ResVal        : word;
  113.                     ComVal        : word;
  114.                     IndVal        : word;    {7}
  115.                     CityTime      : LongInt;    {8&9}
  116.                     CrimeRamp     : word;
  117.                     PolluteRamp   : word;
  118.                     LandValAvg    : word;
  119.                     CrimeAvg      : word;
  120.                     PollutionAvg  : word;
  121.                     GameLevel     : word;
  122.                     CityClass     : word;
  123.                     CityScore     : word;    {17}
  124.                     Filler1       : Array[1..32] of word;
  125.                     TotalFunds    : LongInt;       {50&51}
  126.                     AutoBull      : word;
  127.                     AutoBudget    : word;
  128.                     AutoGo        : word;
  129.                     SoundEffects  : word;
  130.                     TaxRate       : word;
  131.                     SimSpeed      : word;    {57}
  132.                     PoliceBudget  : LongInt;
  133.                     FireBudget    : LongInt;
  134.                     RoadBudget    : LongInt;       {62&63}
  135.                     Filler2       : Array[1..56] of word;  {120}
  136.                   end;
  137.  
  138.   Map_Rec        = Array [1..120,1..100] of word;  {the SimCity Map}
  139.   Char64         = Array [1..64] of char;
  140.   Char480        = Array [1..480] of char;
  141.  
  142.   {The Record that holds the complete SimCity File}
  143.   SimCity_Rec    = Record
  144.                      CityName   : Char64;
  145.                      MacInfo    : Char64;
  146.                      ResHis     : Char480;
  147.                      ComHis     : Char480;
  148.                      IndHis     : Char480;
  149.                      CrimeHis   : Char480;
  150.                      PolluteHis : Char480;
  151.                      CashFlowHis: Char480;
  152.                      MiscVar    : MiscVar_Rec;
  153.                      Map        : Map_Rec;
  154.  
  155.                    end;
  156.  
  157. Var
  158.   SimCityFname     : Str40;                        {SimCity File name}
  159.   SimCityRecord    : SimCity_Rec;                  {SimCity File Record}
  160.   SimCityFile      : File of SimCity_Rec;          {SimCity File}
  161.   MapRecord        : MapTileIndex_Rec;             {Map Tile INDEX records}
  162.   MapAttrRecord    : MapTileAttr_Rec;              {Map Tile Attribute record}
  163.  
  164. Procedure ReadSimCityFile;
  165. Procedure WriteSimCityFile;
  166. Procedure MapTileIndexValues;
  167.  
  168. Implementation
  169.  
  170. {****************************************************************************}
  171. Function SwapLong(L : LongInt) : LongInt;
  172. {Converts a 32 bit word from "Mototola" format to "Intel" format}
  173. {
  174.   Tp-inline code from Pat Ritchey
  175.   The first 2 pops accomplish the swap of the hi and low words and
  176.   the XCHGs do the equivalent of the TP swap() function
  177. }
  178.  
  179. inline(
  180.        $5A/       { POP   DX    }
  181.        $58/       { POP   AX    }
  182.        $86/$E0/   { XCHG  AH,AL }
  183.        $86/$F2    { XCHG  DH,DL }
  184.        );
  185.  
  186. {****************************************************************************}
  187. Procedure SwapVariables;
  188. { after reading a SimCity file, the function will change all word/longint
  189.   variables from "Mototola" format to "Intel" format or vise versa}
  190.  
  191. var
  192.   i, k  : Byte;
  193.  
  194. begin
  195.   With SimCityRecord.MiscVar do
  196.     begin
  197.       filler        := swap(filler);
  198.       ExtMktSize    := swap(ExtMktSize);
  199.       ResPop        := swap(ResPop);
  200.       ComPop        := swap(ComPop);
  201.       IndPop        := swap(IndPop);
  202.       ResVal        := swap(ResVal);
  203.       ComVal        := swap(ComVal);
  204.       IndVal        := swap(IndVal);
  205.       CityTime      := swaplong(CityTime);
  206.       CrimeRamp     := swap(CrimeRamp);
  207.       PolluteRamp   := swap(PolluteRamp);
  208.       LandValAvg    := swap(LandValAvg);
  209.       CrimeAvg      := swap(CrimeAvg);
  210.       PollutionAvg  := swap(PollutionAvg);
  211.       GameLevel     := swap(GameLevel);
  212.       CityClass     := swap(CityClass);
  213.       CityScore     := swap(CityScore);
  214.       for i := 1 to 32 do Filler1[i] := swap(Filler1[i]);
  215.       TotalFunds    := swaplong(TotalFunds);
  216.       AutoBull      := swap(AutoBull);
  217.       AutoBudget    := swap(AutoBudget);
  218.       AutoGo        := swap(AutoGo);
  219.       SoundEffects  := swap(SoundEffects);
  220.       TaxRate       := swap(TaxRate);
  221.       SimSpeed      := swap(SimSpeed);
  222.       PoliceBudget  := swaplong(PoliceBudget);
  223.       FireBudget    := swaplong(FireBudget);
  224.       RoadBudget    := swaplong(RoadBudget);
  225.       for i := 1 to 56 do Filler2[i] := swap(Filler2[i]);
  226.     end;
  227.   With SimCityRecord do
  228.     for i := 1 to 120 do
  229.       for k := 1 to 100 do
  230.         Map[i,k] := swap(Map[i,k]);
  231.  
  232. end;
  233. {****************************************************************************}
  234. Procedure ReadSimCityFile;
  235. {
  236.   Reads a SimCity file into SimCityRecord and swaps all word/longint
  237.   into intel format. CAUTION: This procedure ASSUMEs a VALID filename!
  238. }
  239.  
  240. begin
  241.   Assign(SimCityFile, SimCityFname);
  242.   Reset(SimCityFile);
  243.   Read(SimCityFile, SimCityRecord);
  244.   Close(SimCityFile);
  245.   SwapVariables;
  246. end;
  247.  
  248. {****************************************************************************}
  249. Procedure WriteSimCityFile;
  250. {
  251.   Swaps all word/longint variable from Intel to Mototola format and
  252.   writes a SimCity file back to disk
  253. }
  254.  
  255. begin
  256.   SwapVariables;
  257.   Assign(SimCityFile, SimCityFname);
  258.   Rewrite(SimCityFile);
  259.   Write(SimCityFile, SimCityRecord);
  260.   Close(SimCityFile);
  261. end;
  262.  
  263. {****************************************************************************}
  264. Function MapTileIndex(W : Word) : Word;
  265. {
  266.   Retrieves the tile index number from a map tile. This number is the
  267.   lower 10 bits of the integer. The upper 6 bits are tile attribute flags
  268.   and can be read by MapTileAttr(). The Procedure MapTileIndexValues()
  269.   is used to read the index value of each map square
  270. }
  271.  
  272. begin
  273.   MapTileIndex := W and $03FF;
  274. end;
  275.  
  276. {****************************************************************************}
  277. Function MapTileAttr(W : Word; FlagNo : Byte) : Boolean;
  278. {
  279.   FlagNo ranges from 0 to 5. These are the upper 6 bits of the map
  280.   tile integer. This function could be writen much more efficient by
  281.   passing the entire MapAttrRecord variable and changing it in one pass.
  282.   However, this function is written this way to allow any manipulation
  283.   you may want. In general these attributes are NOT very useful, so this
  284.   function is NOT activated in this unit.
  285. }
  286.  
  287. begin
  288.   MapTileAttr := False;
  289.   Case FlagNo of
  290.     1 : if (W AND $0400 > 0) then MapTileAttr := True; {zoned}
  291.     2 : if (W AND $0800 > 0) then MapTileAttr := True; {animated}
  292.     3 : if (W AND $1000 > 0) then MapTileAttr := True; {bulldozable}
  293.     4 : if (W AND $2000 > 0) then MapTileAttr := True; {burnable}
  294.     5 : if (W AND $4000 > 0) then MapTileAttr := True; {conductPower}
  295.     6 : if (W AND $8000 > 0) then MapTileAttr := True; {powered}
  296.   end;
  297. end;
  298.  
  299. {****************************************************************************}
  300. Procedure MapTileIndexValues;
  301. {
  302.   Retrieves the INDEX value for each map tile (square)
  303. }
  304.  
  305. var
  306.   i, k : Byte;
  307.   Index: word;
  308.  
  309. begin
  310.   FillChar(MapRecord, SizeOf(MapRecord), 0);
  311.   With SimCityRecord do
  312.     for i := 1 to 120 do
  313.       for k := 1 to 100 do
  314.         begin
  315.           index := MapTileIndex(Map[i,k]);
  316.           With MapRecord do
  317.             Case Index of
  318.                     0 : inc(ClearTerrain);
  319.                     2 : inc(Water);
  320.                     4 : inc(River);
  321.                 5..20 : inc(RiverEdge);
  322.                21..36 : inc(TreeEdge);
  323.                    37 : inc(Trees);
  324.                40..43 : inc(Parks);
  325.                44..47 : inc(Rubble);
  326.                48..51 : inc(Flood);
  327.                    52 : inc(Radiation);
  328.                56..63 : inc(Fire);
  329.                64..78 : inc(Road_NoTraf);
  330.               80..142 : inc(Road_LtTraf);
  331.              144..206 : inc(Road_HvTraf);
  332.              208..222 : inc(Power);
  333.              224..238 : inc(Transit);
  334.                   244 : inc(EmptyRes);
  335.              249..260 : inc(Houses);
  336.                   265 : inc(ResLoVal1);
  337.                   274 : inc(ResLoVal2);
  338.                   283 : inc(ResLoVal3);
  339.                   292 : inc(ResLoVal4);
  340.                   301 : inc(ResMidVal1);
  341.                   310 : inc(ResMidVal2);
  342.                   319 : inc(ResMidVal3);
  343.                   328 : inc(ResMidVal4);
  344.                   337 : inc(ResUpVal1);
  345.                   346 : inc(ResUpVal2);
  346.                   355 : inc(ResUpVal3);
  347.                   364 : inc(ResUpVal4);
  348.                   373 : inc(ResHiVal1);
  349.                   382 : inc(ResHiVal2);
  350.                   391 : inc(ResHiVal3);
  351.                   400 : inc(ResHiVal4);
  352.                   409 : inc(Hospital);
  353.                   418 : inc(Church);
  354.                   427 : inc(EmptyCom);
  355.                   436 : inc(ComLoVal1);
  356.                   445 : inc(ComLoVal2);
  357.                   454 : inc(ComLoVal3);
  358.                   463 : inc(ComLoVal4);
  359.                   472 : inc(ComLoVal5);
  360.                   481 : inc(ComMidVal1);
  361.                   490 : inc(ComMidVal2);
  362.                   499 : inc(ComMidVal3);
  363.                   508 : inc(ComMidVal4);
  364.                   517 : inc(ComMidVal5);
  365.                   526 : inc(ComUpVal1);
  366.                   535 : inc(ComUpVal2);
  367.                   544 : inc(ComUpVal3);
  368.                   553 : inc(ComUpVal4);
  369.                   562 : inc(ComUpVal5);
  370.                   571 : inc(ComHiVal1);
  371.                   580 : inc(ComHiVal2);
  372.                   589 : inc(ComHiVal3);
  373.                   598 : inc(ComHiVal4);
  374.                   607 : inc(ComHiVal5);
  375.                   616 : inc(IndEmpty);
  376.                   625 : inc(IndLoVal1);
  377.                   634 : inc(IndLoVal2);
  378.                   643 : inc(IndLoVal3);
  379.                   652 : inc(IndLoVal4);
  380.                   661 : inc(IndHiVal1);
  381.                   670 : inc(IndHiVal2);
  382.                   679 : inc(IndHiVal3);
  383.                   688 : inc(IndHiVal4);
  384.                   698 : inc(SeaPort);
  385.                   716 : inc(Airport);
  386.                   750 : inc(CoalPower);
  387.                   765 : inc(FireStation);
  388.                   774 : inc(PoliceStation);
  389.                   784 : inc(StadiumEmpty);
  390.                   800 : inc(StadiumFull);
  391.                   816 : inc(NuclearPower);
  392.              828..831 : inc(OpenH_Bridge);
  393.              832..839 : inc(RadarDish);
  394.              840..843 : inc(Fountain);
  395.              948..951 : inc(OpenV_Bridge);
  396.             end; {case}
  397.         end;   {for k}
  398. end;
  399.  
  400. {****************************************************************************}
  401. end.